import { useUserStore } from '@/stores/modules/user.js'
import { createRouter, createWebHistory } from 'vue-router'


//import { useUserStore } from '@/stores/user.js'

// createRouter 创建路由实例
// 配置 history 模式
// 1. history模式：createWebHistory     地址栏不带 #
// 2. hash模式：   createWebHashHistory 地址栏带 #
// console.log(import.meta.env.DEV)

// vite 中的环境变量 import.meta.env.BASE_URL  就是 vite.config.js 中的 base 配置项
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/res',
      redirect: '/school/test2',
      children: [
        // {
        //   path: '/register',
        //   name: 'register',
        //   component: () => import('@/views/Enterprise/RegisterView.vue'),
         
        // }, 
        // {
          
        //   path: '/contract',
        //   name: 'contract',
        //   component: () => import('@/views/Enterprise/ContractView.vue')
        // },
        // {
        //   path: '/archive',
        //   name: 'archive',
        //   component: () => import('@/views/Enterprise/ArchiveView.vue')
        // },
        // {
        //   path:'/index',
        //   name:'index',
        //   component: () => import('@/views/Enterprise/IndexView.vue')
        // },
        {
          path: '/index',
          name: 'index',
          component: () => import('@/views/Qiye/test.vue')
        }
      ]
    },
    {
      path: '/',  // 确保该路径存在
      // name: 'test',
      component: () => import('@/components/layout/AppLayout.vue'),
      children:[
        {
          path: '/register',
          name: 'register',
          component: () => import('@/views/Enterprise/RegisterView.vue')
        }, 
        {
          
          path: '/contract',
          name: 'contract',
          component: () => import('@/views/Enterprise/ContractView.vue')
        },
        {
          path: '/archive',
          name: 'archive',
          component: () => import('@/views/Enterprise/ArchiveView.vue')
        },
        {
          path:'/index',
          name:'index',
          component: () => import('@/views/Enterprise/IndexView.vue')
        },
        {
          path: '/school/test6',
          component: () => import('@/views/Qiye/test6.vue')
        },
        {
          path: '/school/Dashboard',
          component: () => import('@/views/Qiye/Dashboard.vue')
        },
        {
          path: '/finance',
          component: () => import('@/views/Enterprise/FinanceView.vue')
        },
        {
          path: '/service',
          component: () => import('@/views/Enterprise/ServiceView.vue')
        }
      ]
    },
    
    {
      //学校系统
      path: '/school',
      component: () => import('@/views/Home/Header.vue'),
      children: [
        // {
        //   path: '/school/backstage',
        //   component: () => import('@/views/School/BackStage.vue')
        // },
        {
          //个人中心模块
          path: '/school/center',
          component: () => import('@/views/Home/Menu.vue'),
          children: [
            {
              path: '/school/test',
              component: () => import('@/views/Qiye/test.vue')
            },
            {
              path: '/school/test2',
              component: () => import('@/views/Qiye/test2.vue')
            },
            {
              path: '/school/test3',
              component: () => import('@/views/Qiye/test3.vue')
            },
            {
              path: '/school/test4',
              component: () => import('@/views/Qiye/test4.vue')
            },
            {
              path: '/school/test5',
              component: () => import('@/views/Qiye/test5.vue')
            },
            {
              path: '/school/test6',
              component: () => import('@/views/Qiye/test6.vue')
            },
            {
              path: '/school/test7',
              component: () => import('@/views/Qiye/test7.vue')
            },
            {
              path: '/school/test8',
              component: () => import('@/views/Qiye/test8.vue')
            },
            {
              path: '/school/Dashboard',
              component: () => import('@/views/Qiye/Dashboard.vue')
            },
            {
              path: '/school/test10',
              component: () => import('@/views/Qiye/test10.vue')
            }
          ]
        }
      ]
    }
  ]
})
// router.beforeEach(async (to, from, next) => {
//   // 在路由守卫中动态获取用户 token
//   //console.log('路由')
//   const userStore = useUserStore()
//   await userStore.getUser() // 这里可能需要您定义一个方法来从本地存储中获取用户信息
//   const userToken = userStore.User.token
//   //console.log(userToken)
//   const whiteList = [
//     '/school/test',
//     '/register',
//     '/login',
//     '/index',
//     '/about',
//     '/TeacherProfile',
//     '/new',
//     'NewsReader',
//     '/test',
//     '/consultation',
//     '/home/Experts',
//     '/home/new',
//     '/NewsReader'
//   ]

//   if (userToken) {
//     // if (to.path === '/login') {
//     //   next('/home')
//     // } else {
//     //   next()
//     // }

//     if (userStore.User.userRoleId) {
//       // 检查用户所属系统，例如从用户信息中获取角色或权限信息
//       const userRole = userStore.User.userRoleId // 假设用户角色信息存在于User对象中
//       const allowedPaths = getAllowedPaths(userRole) // 获取用户被允许访问的路径列表
//       const isAllowedPath = allowedPaths.some((path) => {
//         // 使用正则表达式匹配包括动态段的模式
//         const regex = path.replace(/:\w+/g, '\\w+')
//         return new RegExp(`^${regex}$`).test(to.path)
//       })

//       if (isAllowedPath) {
//         next()
//       } else {
//         // 如果用户尝试访问不属于其所属系统的页面，则重定向到其系统的首页或其他适当页面
//         redirectToSystemHome(userRole, next)
//       }
//     }
//   } else {
//     if (whiteList.includes(to.path)) {
//       next()
//     } else {
//       next('/login')
//     }
//   }
// })
// 根据用户角色获取其被允许访问的路径列表
function getAllowedPaths(role) {
  // 根据角色返回相应的被允许访问的路径列表
  //console.log('yongle')
  switch (role) {
    //学校
    case 2:
      return [
        '/school/test',
        '/school/test2',
        '/school/test3',
        '/school/test4',
        '/school/test5',
        '/school/test6',
        '/school/test7',
        '/school/test8',
        '/school/Dashboard',
        '/school/test10',
        '/user/password',
        '/reportPreview',
        '/manage/report',
        '/school/center/grade/schoolReportPreview',
        '/manage/StudentQuestionnaire',
        '/manage/classStudent',
        '/manage/class',
        '/manage/school/student',
        '/home/Experts',
        '/home/new',
        '/consultation',
        '/NewsReader',
        '/new',
        '/TeacherProfile',
        '/about',
        '/',
        '/login',
        '/register',
        '/index',
        '/questionnaire/:id',
        '/questionnaire2/:id',
        '/school',
        '/school/home',
        '/school/backstage',
        '/school/design/:id',
        '/school/center',
        '/school/ScaleContent',
        '/school/ScaleManagement',
        '/school/center/ConsultiveManagement',
        '/reportPage',
        '/school/center/schoolPreview',
        '/school/center/EvaluationManagement',
        '/school/center/Home',
        '/school/center/SchoolManagement',
        '/school/center/SchoolManagement/student',
        '/school/center/SchoolManagement/studentList',
        '/school/center/SchoolManagement/teacher',
        '/school/center/WarnManage/Student',
        '/school/center/WarnManage/Student',
        '/school/center/WarnManage/logInterview',
        '/school/center/WarnManage/logInterview',
        '/school/center/WarnManage/disturbCrisis',
        '/school/center/WarnManage/disturbCrisis',
        '/school/center/SystemSettings',
        '/school/center/data',
        '/school/center/student',
        '/school/center/schoolPreview',
        '/school/center/student/schoolReportPreview',
        '/school/center/teacher',
        '/school/center/teacher/schoolReportPreview',
        '/school/center/user/profile',
        '/school/center/user/home',
        '/school/center/user/avatar',
        '/school/center/user/password',
        '/school/center/result',
        '/manage/school/warnreport'
      ]
    //学生
    case 4:
      return [
        '/home/Experts',
        '/home/new',
        '/consultation',
        '/NewsReader',
        '/new',
        '/TeacherProfile',
        '/about',
        '/',
        '/login',
        '/register',
        '/index',
        '/student',
        '/student/home',
        '/student/tests',
        '/student/center',
        '/student/center/information',
        '/student/center/consult',
        '/student/center/notice',
        '/student/center/manage',
        '/questionnaire2/:id',
        '/thanks',
        '/thanks2',
        '/student/tests/test'
      ] // 示例，学校系统允许访问的路径
    //超级管理员
    case 1:
      return [
        '/manage/student3',
        '/manage/school/student2',
        '/home/Experts',
        '/home/new',
        '/consultation',
        '/test',
        '/NewsReader',
        '/new',
        '/TeacherProfile',
        '/about',
        '/',
        '/login',
        '/index',
        '/manage/report',
        '/register',
        '/questionnaire/:id',
        '/questionnaire2/:id',
        '/thanks',
        '/thanks2',
        '/home',
        '/reportPreview',
        '/reportPage',
        '/school/report',
        '/classReport',
        '/test/psychological',
        '/test/result',
        '/user/profile',
        '/user/home',
        '/user/avatar',
        '/user/password',
        '/test/openQuestionnaireh',
        '/test/ScaleManagement',
        '/test/ScaleContent',
        '/appraisal/PersonalAssessment',
        '/appraisal/SchoolEvaluation',
        '/file/StudentFile',
        '/appraisal/publish',
        '/questionnaire/HomePage',
        '/manage/manage',
        '/manage/school',
        '/manage/student',
        '/manage/allUser',
        '/appraisal/school',
        '/test/design/:id'
      ]
    default:
      return [] // 其他角色或未定义角色默认不允许访问任何路径
  }
}
// 根据用户角色重定向到对应系统的首页或其他适当页面
function redirectToSystemHome(role, next) {
  // 根据用户角色重定向到对应系统的首页或其他适当页面
  switch (role) {
    case 1:
      next('/home') // 示例，重定向到学生系统的首页
      break
    case 4:
      next('/student/home') // 示例，重定向到学校系统的首页
      break
    case 2:
      next('/school/home') // 示例，重定向到学校系统的首页
      break
    default:
      next() // 其他角色或未定义角色默认重定向到首页
  }
}
export default router